package com.lemontree.mbg;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Lemontree
 * @Description: 自定义通用mapper
 * @Date: 20:22 2018/5/7
 */
public class MbgHelper {
    private static final String MBG_XML_CONFIG = "/MBG.xml";

    public static void generate() throws IOException, XMLParserException,
            InvalidConfigurationException, SQLException,
            InterruptedException {
        // 记得导对应包
        // 对应传入plugins里面，作为条件，塞选对应需要的插件
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(
                MbgHelper.class.getResourceAsStream(MBG_XML_CONFIG));
        DefaultShellCallback callback = new DefaultShellCallback(
                overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                callback, warnings);
        myBatisGenerator.generate(null);
    }

    public static void main(String[] args) throws InterruptedException,
            SQLException, InvalidConfigurationException,
            XMLParserException, IOException {
        try {
            generate();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        System.out.println("完成！");
    }

}
